package com.zidian.study.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.zidian.study.bean.StudyCourseTable;
import com.zidian.study.globa.BaseFragment;
import com.zidian.study.globa.Constant;
import com.zidian.study.utils.GetUserInfo;
import com.zidian.study.utils.ToastUtil;
import com.zidian.study.view.ClassInfo;
import com.zidian.study.view.ScheduleView;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import greenDAO.bean.Course;
import okhttp3.Call;
import com.zidian.student.trucks.R;


/**
 * Created by Administrator on 2016/8/23 0023.
 * 课程表
 */
public class CourseTableFragment extends BaseFragment {

    private ScheduleView scheduleView;
    private ArrayList<ClassInfo> classList;
    private int currentWeek;
    private ArrayList<StudyCourseTable.DataBean> dataList = new ArrayList<>();

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        scheduleView = (ScheduleView) view.findViewById(R.id.scheduleView);

        List<Course> courseList = new ArrayList<>();
            try {
                getCourseTable();
            } catch (IOException e) {
                e.printStackTrace();
            }

        //课程表点击事件(弹出选择老师Dialog)
        scheduleView.setOnItemClassClickListener(new ScheduleView.OnItemClassClickListener() {
                    @Override
                    public void onClick(ClassInfo classInfo) {
                        Log.i("classInfo",classInfo.toString());
                        final List<ClassInfo.Teacher> teachers = classInfo.getTeachers();


                    }
                });
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_course;
    }

    public static CourseTableFragment newInstance(int currentWeek) {


        CourseTableFragment fragment = new CourseTableFragment();
        fragment.currentWeek = currentWeek;

        return fragment;
    }

    private int getBeginClass(String myclass) {

        if (myclass.indexOf(",") == -1) {
            return Integer.parseInt(myclass);
        } else {
            String b = myclass.substring(0, myclass.indexOf(","));
            int i = Integer.parseInt(b);
            return i;
        }
    }

    private int getClassLength(String myclass) {
        if (myclass.lastIndexOf(",") != -1) {
            String c = myclass.substring(myclass.lastIndexOf(",") + 1, myclass.length());
            String b = myclass.substring(0, myclass.indexOf(","));
            int i = Integer.parseInt(b);
            int j = Integer.parseInt(c);
            return j - i + 1;
        } else {
            return 1;
        }

    }

    private int getWeekDay(String weeklyDay) {

        int day = 1;

        if (weeklyDay.equals("周一")) {
            day = 1;
        } else if (weeklyDay.equals("周二")) {
            day = 2;
        } else if (weeklyDay.equals("周三")) {
            day = 3;
        } else if (weeklyDay.equals("周四")) {
            day = 4;
        } else if (weeklyDay.equals("周五")) {
            day = 5;
        } else if (weeklyDay.equals("周六")) {
            day = 6;
        } else if (weeklyDay.equals("周日")) {
            day = 7;
        }
        return day;

    }

    /**
     * 获取课程表信息
     * @throws IOException
     */
    private void getCourseTable() throws IOException {
        String loginUrl = Constant.URL_GOAL + "stuCou/select";
        HashMap<String, String> map = new HashMap<>();
        map.put("studentId", GetUserInfo.getStudentId(mActivity));
        map.put("token", GetUserInfo.getToken(mActivity));

        OkHttpUtils.post().url(loginUrl).params(map).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                ToastUtil.showToast(mActivity, "获取课程表失败");
            }

            @Override
            public void onResponse(String response, int id) {
                Gson gson = new Gson();

                StudyCourseTable data = gson.fromJson(response, StudyCourseTable.class);
                if (data.isSuccess()) {
                    dataList = (ArrayList<StudyCourseTable.DataBean>) data.getData();
                    classList = new ArrayList<ClassInfo>();
                    for (int i = 0; i < data.getData().size(); i++) {
                        ClassInfo classInfo = new ClassInfo();
                        if (Integer.parseInt(dataList.get(i).getBeginEndWeek()) == currentWeek) {
                            classInfo.setClassname(dataList.get(i).getCourseName());
                            classInfo.setFromClassNum(getBeginClass(dataList.get(i).getWeeklyQuarter()));
                            classInfo.setClassNumLen(getClassLength(dataList.get(i).getWeeklyQuarter()));
                            classInfo.setClassRoom(dataList.get(i).getClassRoom());
                            classInfo.setWeekday(getWeekDay(dataList.get(i).getWeeklyDay()));
                            classInfo.setBeginEndWeek(dataList.get(i).getBeginEndWeek());
                            //获取其中的老师
                            List<ClassInfo.Teacher> teachers = new ArrayList<ClassInfo.Teacher>();
                            if (dataList.get(i).getTeachers() == null) {
                                return;
                            }
                            for (int j = 0; j < dataList.get(i).getTeachers().size(); j++) {
                                ClassInfo.Teacher teacher = new ClassInfo.Teacher();
                                teacher.setTeacherId(dataList.get(i).getTeachers().get(j).getTeacherId());
                                teacher.setTeacherName(dataList.get(i).getTeachers().get(j).getTeacherName());
                                teachers.add(teacher);
                            }
                            classInfo.setTeachers(teachers);
                            classList.add(classInfo);
                        }

                    }
                    scheduleView.setClassList(classList);// 将课程信息导入到课表中
                }
            }
        });
    }

    /**
     * @param week
     * @return
     */
    public List<Integer> getWeekCount(String week) {

        List<Integer> list = new ArrayList<>();
        String c = week.substring(week.lastIndexOf("-") + 1, week.length());
        String b = week.substring(0, week.indexOf("-"));
        int i = Integer.parseInt(b);
        int j = Integer.parseInt(c);

        for (int k = 0; k <= j - i + 1; k++) {

            if (j >= i + k) {
                list.add(i + k);
            }


        }
        return list;
    }

}
